package campus.service.impl;

import campus.domain.CampusTransferRewarder;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import campus.repository.CampusTransferRewarderRepository;
import campus.service.CampusTransferRewarderService;
import campus.service.dto.CampusTransferRewarderDto;
import campus.service.dto.CampusTransferRewarderQueryCriteria;
import campus.service.mapper.CampusTransferRewarderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Service
//@CacheConfig(cacheNames = "campusTransferRewarder")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CampusTransferRewarderServiceImpl implements CampusTransferRewarderService {

    private final CampusTransferRewarderRepository campusTransferRewarderRepository;

    private final CampusTransferRewarderMapper campusTransferRewarderMapper;

    public CampusTransferRewarderServiceImpl(CampusTransferRewarderRepository campusTransferRewarderRepository, CampusTransferRewarderMapper campusTransferRewarderMapper) {
        this.campusTransferRewarderRepository = campusTransferRewarderRepository;
        this.campusTransferRewarderMapper = campusTransferRewarderMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CampusTransferRewarderQueryCriteria criteria, Pageable pageable){
        Page<CampusTransferRewarder> page = campusTransferRewarderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(campusTransferRewarderMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CampusTransferRewarderDto> queryAll(CampusTransferRewarderQueryCriteria criteria){
        return campusTransferRewarderMapper.toDto(campusTransferRewarderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CampusTransferRewarderDto findById(Long id) {
        CampusTransferRewarder campusTransferRewarder = campusTransferRewarderRepository.findById(id).orElseGet(CampusTransferRewarder::new);
        ValidationUtil.isNull(campusTransferRewarder.getId(),"CampusTransferRewarder","id",id);
        return campusTransferRewarderMapper.toDto(campusTransferRewarder);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CampusTransferRewarderDto create(CampusTransferRewarder resources) {
        return campusTransferRewarderMapper.toDto(campusTransferRewarderRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CampusTransferRewarder resources) {
        CampusTransferRewarder campusTransferRewarder = campusTransferRewarderRepository.findById(resources.getId()).orElseGet(CampusTransferRewarder::new);
        ValidationUtil.isNull( campusTransferRewarder.getId(),"CampusTransferRewarder","id",resources.getId());
        campusTransferRewarder.copy(resources);
        campusTransferRewarderRepository.save(campusTransferRewarder);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            campusTransferRewarderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CampusTransferRewarderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CampusTransferRewarderDto campusTransferRewarder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发布用户id", campusTransferRewarder.getRewarderId());
            map.put("悬赏的名字", campusTransferRewarder.getRewardName());
            map.put("悬赏分类", campusTransferRewarder.getRewardType());
            map.put("单价", campusTransferRewarder.getBankType());
            map.put("标签", campusTransferRewarder.getLabel());
            map.put("数量", campusTransferRewarder.getAmount());
            map.put("提交期限", campusTransferRewarder.getSubmitTime());
            map.put("详情", campusTransferRewarder.getDetails());
            map.put("0-发布，1-撤回，2-完成(状态）", campusTransferRewarder.getState());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}